﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace KD.Sms.UI_Query
{
    public partial class frmSamplingPosInfo : Form
    {
        private DataTable E_SourceChart = null;
        private DataTable E_SourceGrid = null;
        public frmSamplingPosInfo()
        {
            InitializeComponent();
        }




        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            if (e.Tool.Key == "Query")
            {
                DoBind();
            }
            else if (e.Tool.Key == "OutPut")
            {
                DoOutPut();
            }
            else if (e.Tool.Key == "Clear")
            {
                txtMaterial.SelectedIndex = -1;
                txtSupplier.SelectedIndex = -1;
                dtBegin.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
                dtEnd.Value = DateTime.Now.AddSeconds(5);
            }
        }

        private SeriesChartType E_Type { set; get; }
        private void frmSamplingInfo_Load(object sender, EventArgs e)
        {
            dtBegin.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
            dtEnd.Value = DateTime.Now.AddSeconds(5);

            txtMaterial.DataSource = DAL.SqlLite.Main.Instance.DoQueryMaterial();
            txtMaterial.DisplayMember = "Material_Name";
            txtMaterial.ValueMember = "Material_id";
            txtMaterial.SelectedIndex = -1;

            txtSupplier.DataSource = DAL.SqlLite.Main.Instance.DoQuerySupplier();
            txtSupplier.DisplayMember = "Supplier_Name";
            txtSupplier.ValueMember = "Supplier_Id";
            txtSupplier.SelectedIndex = -1;

            //曲线图
            chartMain.Series.Clear();
            E_Type = SeriesChartType.Spline;
            ChartHelper.AddSeries(chartMain, "曲线图", E_Type, Color.Teal, Color.Red, true);
            //ChartHelper.SetTitle(chart1, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartMain, Color.Transparent, Color.Green);
            ChartHelper.SetLegend(chartMain, Docking.Top, StringAlignment.Center, Color.Transparent, Color.Red);
            ChartHelper.SetXY(chartMain, "块号", "值", StringAlignment.Center, Color.Green, Color.Transparent, AxisArrowStyle.None, 1, 2);
            ChartHelper.SetBorderWidth(chartMain, 3);
            ChartHelper.SetMajorGrid(chartMain, Color.Gray, 2000, 0);

        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoOutPut()
        {
            SaveFileDialog sfExcel = new SaveFileDialog();
            sfExcel.Title = "另存为";
            sfExcel.FileName = string.Format("{0}{1}采样数据导出.xls", (string.IsNullOrEmpty(CacheUser.Chinese_name) ? "采样信息" : CacheUser.Chinese_name), DateTime.Now.ToString("yyMMdd")); //设置默认另存为的名字，可选
            sfExcel.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            sfExcel.AddExtension = true;
            if (sfExcel.ShowDialog() == DialogResult.OK)
            {
                DataTable _tabExcel = E_SourceGrid.Copy();
                int _iStep = 1;
                foreach (DataRow dr in _tabExcel.Rows)
                {
                    dr["Row_Number"] = _iStep;
                    _iStep++;
                }
                string[] strFields = { "Row_Number", "car_no", "samplingPos", "Pos_count", "SampleWeight", "supplier_name", "kind_Name", "Material_Name", "Sampling_Time", "Sampling_time_end", "Sampling_User" };
                string[] strFieldsName = { "序号", "车牌号", "布点信息", "采样点数", "样品重量", "供应商", "物料", "规格", "开始时间", "结束时间", "采样员" };
                NPOIHelper.ExportExcel(_tabExcel, string.Empty, "采样信息", strFields, strFieldsName, "采样信息", sfExcel.FileName);
                MessageBox.Show("导出成功!");
            }

        }

        /// <summary>
        /// 
        /// </summary>
        private void DoBind()
        {
            DoInitSource();
            int _iBlockNo = 0, _iPosCount = 0;
            DataTable _tabSource = DAL.SqlLite.Main.Instance.DoQuerySamplingPosStatic(dtBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")
                                    , txtMaterial.SelectedIndex < 0 ? string.Empty : txtMaterial.SelectedValue.ToString()
                                    , txtSupplier.SelectedIndex < 0 ? string.Empty : txtSupplier.SelectedValue.ToString());
            if (_tabSource != null && _tabSource.Rows.Count > 0)
            {
                foreach (DataRow dr in _tabSource.Rows)
                {
                    _iBlockNo = int.Parse(dr["Block_NO"].ToString());
                    _iPosCount = int.Parse(dr["Pos_Count"].ToString());
                    if (_iBlockNo <= 18)
                    {
                        E_SourceChart.Select(string.Format(" BlockNo = '块{0}' ", _iBlockNo))[0]["PosCount"] = _iPosCount;
                        if (E_SourceGrid.Columns.Contains(string.Format("BlockNo{0}", _iBlockNo)))
                            E_SourceGrid.Rows[0][string.Format("BlockNo{0}", _iBlockNo)] = _iPosCount;
                    }
                }
            }

            gridMain.DataSource = E_SourceGrid;
            gridMain.ClearSelection();

            List<int> _listBlock = new List<int>();
            List<int> _listPosCount = new List<int>();
            foreach (DataRow dr in E_SourceChart.Rows)
            {
                _listBlock.Add(int.Parse(dr["BlockNo"].ToString().Replace("块", "")));
                _listPosCount.Add(int.Parse(dr["PosCount"].ToString()));
            }
            ChartHelper.SetXInterval(chartMain, Math.Ceiling(_listPosCount.Max() * 0.25));
            chartMain.Series[0].Points.DataBindXY(_listBlock, _listPosCount);
        }



        private void frmSamplingInfoSimple_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
                this.Close();
        }


        /// <summary>
        /// 初始化表
        /// </summary>
        private void DoInitSource()
        {
            E_SourceGrid = new DataTable();
            DataColumn dc;
            for (int i = 1; i <= 18; i++)
            {
                dc = new DataColumn();
                dc.ColumnName = "BlockNo" + i.ToString();
                E_SourceGrid.Columns.Add(dc);
            }
            E_SourceGrid.AcceptChanges();

            DataRow dr = E_SourceGrid.NewRow();
            for (int i = 1; i <= 18; i++)
            {
                dr["BlockNo" + i.ToString()] = 0;
            }
            E_SourceGrid.Rows.Add(dr);
            E_SourceGrid.AcceptChanges();

            E_SourceChart = new DataTable();
            dc = new DataColumn();
            dc.ColumnName = "BlockNo";
            E_SourceChart.Columns.Add(dc);

            dc = new DataColumn();
            dc.ColumnName = "PosCount";
            E_SourceChart.Columns.Add(dc);
            E_SourceChart.AcceptChanges();

            for (int i = 1; i <= 18; i++)
            {
                dr = E_SourceChart.NewRow();
                dr["BlockNo"] = "块" + i.ToString();
                dr["PosCount"] = 0;
                E_SourceChart.Rows.Add(dr);
            }
            E_SourceChart.AcceptChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chart1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (E_Type == SeriesChartType.Column)
            {
                E_Type = SeriesChartType.Spline;
                chartMain.Series.Clear();
                ChartHelper.AddSeries(chartMain, "曲线图", E_Type, Color.Teal, Color.Red, true);
                //ChartHelper.SetTitle(chart1, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
                ChartHelper.SetStyle(chartMain, Color.Transparent, Color.Green);
                ChartHelper.SetLegend(chartMain, Docking.Top, StringAlignment.Center, Color.Transparent, Color.Red);
                ChartHelper.SetXY(chartMain, "块号", "值", StringAlignment.Center, Color.Green, Color.Green, AxisArrowStyle.None, 1, 2);
                ChartHelper.SetBorderWidth(chartMain, 3);
                ChartHelper.SetMajorGrid(chartMain, Color.Gray, 20, 0);
            }
            else
            {
                E_Type = SeriesChartType.Column;
                chartMain.Series.Clear();
                ChartHelper.AddSeries(chartMain, "柱状图", E_Type, Color.Teal, Color.Red, true);
                //ChartHelper.SetTitle(chart1, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
                ChartHelper.SetStyle(chartMain, Color.Transparent, Color.Green);
                ChartHelper.SetLegend(chartMain, Docking.Top, StringAlignment.Center, Color.Transparent, Color.Red);
                ChartHelper.SetXY(chartMain, "块号", "值", StringAlignment.Center, Color.Green, Color.Green, AxisArrowStyle.None, 1, 2);
                ChartHelper.SetMajorGrid(chartMain, Color.Gray, 20, 0);
            }

            DoBind();
        }
    }
}
